const app = getApp();
const ajax = app.myRequest()
import storage from "../../../utils/storage.js";
Page({
    data: {
        menber: [],
        buy_msg: '立即',
    },

    onLoad: function (options) {
        this.setData({ id: options.id })
        this.getMember()
    },

    getMember() {
        var id = this.data.id
        var _this = this
        ajax.getRequest('/Good/index', { id }).then((res) => {
            _this.setData({ menber: res.data.data })
            if (res.data.data.is_discount == 1) {
                _this.setData({ buy_msg: '使用优惠券' })
            }
        })
    },

    getPhoneNumber(e) {
        var encryptedData = e.detail.encryptedData
        var iv = e.detail.iv
        if (e.detail.errMsg == "getPhoneNumber:ok") {
            ajax.getRequest('/User/authPhone', { encryptedData, iv }).then((res) => {
                storage.setStorage('mobile', res.data.data)
                this.setData({ phone: res.data.data })
            })
        }
    },

    purchase() {
        var _this = this
        wx.getSetting({
            success: function (res) {
                if (res.authSetting['scope.userInfo']) {
                    _this.payment()
                } else {
                    wx.navigateTo({ url: '/pages/login/SignIn/index' })
                }
            }
        })
    },

    // 支付
    payment() {
        var _this = this
        var id = _this.data.id
        wx.requestSubscribeMessage({
            tmplIds: ['kvmaVk0OuBOwrZKjTiMJ1je4JK6OeBzrtShS9lqakjU'],
            success(res) {
                if (res['kvmaVk0OuBOwrZKjTiMJ1je4JK6OeBzrtShS9lqakjU'] === 'accept') {
                    ajax.postRequest('/Order/create', { good_id: id }).then((res) => {
                        if (res.data.code == 1) {
                            _this.generateOrder(res.data.data)
                        } else {
                            wx.showModal({
                                title: '警告',
                                content: res.data.msg,
                                showCancel: false,
                            })
                        }
                    })
                }
            },
            complete: function (res) { }
        })
    },

    // 支付
    generateOrder(res) {
        var timeStamp = res.pay.timeStamp
        var appId = res.add.order_id
        var price = res.add.price
        wx.requestPayment({
            timeStamp: res.pay.timeStamp,
            nonceStr: res.pay.nonceStr,
            package: res.pay.package,
            signType: res.pay.signType,
            paySign: res.pay.paySign,
            success: function (res) {
                wx.navigateTo({
                    url: '/pages/personal/successfulPayment/index?timeStamp=' + timeStamp + '&price=' + price + '&appId=' + appId,
                })
            },
            fail: function (res) {
                wx.showToast({ title: '支付失败', icon: 'none' })
                setTimeout(function () {
                    wx.navigateTo({
                        url: '/pages/personal/PaymentFailed/index?timeStamp=' + timeStamp + '&price=' + price + '&appId=' + appId,
                    })
                })
            }
        })
    },

    onShow: function () {
        var token = wx.getStorageSync('token') || null
        this.setData({ token })
        this.getPhone()
    },

    getPhone() {
        var phone = storage.getStorage('mobile') || this.data.phone
    },
})